package com.magicalcoder.youyamvc.web.controller.web.adminuser;
import com.magicalcoder.youyamvc.app.adminuser.service.AdminUserService;
import com.magicalcoder.youyamvc.app.adminuser.service.AdminUserProxyService;
import com.magicalcoder.youyamvc.app.adminuser.constant.AdminUserConstant;
import com.magicalcoder.youyamvc.app.adminuser.util.AdminUserUtil;
import com.magicalcoder.youyamvc.web.controller.web.adminuser.param.AdminUserRequest;
import com.magicalcoder.youyamvc.web.controller.web.adminuser.param.AdminUserResponse;
import com.magicalcoder.youyamvc.app.model.AdminUser;
import com.magicalcoder.youyamvc.core.common.utils.ProjectUtil;
import com.magicalcoder.youyamvc.core.common.utils.MapUtil;
import com.magicalcoder.youyamvc.core.common.utils.ListUtils;
import com.magicalcoder.youyamvc.core.common.utils.StringUtils;
import com.magicalcoder.youyamvc.core.common.dto.AjaxData;
import com.magicalcoder.youyamvc.core.common.utils.copy.Copyer;
import com.magicalcoder.youyamvc.core.spring.admin.AdminLoginController;
import com.magicalcoder.youyamvc.core.spring.web.WebLoginController;
import com.magicalcoder.youyamvc.web.common.BaseController;
import com.magicalcoder.youyamvc.app.userweb.util.UserWebUtil;
import com.magicalcoder.youyamvc.core.common.exception.BusinessException;

import org.springframework.format.annotation.DateTimeFormat;
import java.util.*;
import java.math.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* 代码为自动生成 Created by www.magicalcoder.com
* 如果你改变了此类 read 请将此行删除
* 欢迎加入官方QQ群:323237052
*/

@RequestMapping({"/web/admin_user"})
@Controller
public class AdminUserListController extends BaseController
{
    @Resource
    private AdminUserService adminUserService;
    @Resource
    private AdminUserProxyService adminUserProxyService;

    /** method:GET
    *   url:/web/admin_user/list/{pageIndex}/{pageSize}?...
    *   demo:/web/admin_user/list/1/20
    *   获取管理员表分页数据
    *   是否需要登录                          是
    *是否必须   入参                          注释
    *  是   @pageIndex                     (Integer)当前页码 [1,n]
    *  是   @pageSize                      (Integer)每页条数 [1,n]
    *  否   @callback                      (String)回调方法
    *  否   @orderBySqlField               (String)排序字段
    *  否   @descAsc                       (String)desc|asc
    *  否   @encode                        (String)编码 默认UTF-8
    *  否   @id                     (Long)主键 
    *  否   @userName                     (String)用户名 
    *  否   @password                     (String)密码 
    *  否   @realName                     (String)真名 
    *  否   @email                     (String)邮箱 
    *  否   @telephone                     (String)座机号 
    *  否   @mobilePhone                     (String)手机号 
    *  否   @address                     (String)手机号 
    *  否   @createTime                     (Date)create_time 
    *  否   @updateTime                     (Date)更新时间 
    *  否   @superAdmin                     (Integer)是否超级管理员 [{"":"全部"},{"0":"否"},{"1":"是"}]
    *  否   @roleId                     (Long)角色 
    *  否   @idFirst                     (Long)主键 
    *  否  like @userNameFirst                     (String)用户名 
    *  否  like @realNameFirst                     (String)真名 
    *  否   @emailFirst                     (String)邮箱 
    *  否  >= @createTimeFirst                     (Date)create_time 
    *  否  <= @createTimeSecond                  (Date)create_time 
    *  否  >= @updateTimeFirst                     (Date)更新时间 
    *  否  <= @updateTimeSecond                  (Date)更新时间 
    *  否   @superAdminFirst                     (Integer)是否超级管理员 [{"":"全部"},{"0":"否"},{"1":"是"}]
    *  否  in @roleIdFirst                     (Long)角色 
    *  返回
    *   {
    *      code:0,message:"",jsonp:"",
    *      info:
                [{
    *              id         (Long)主键 
    *              userName         (String)用户名 
    *              password         (String)密码 
    *              realName         (String)真名 
    *              email         (String)邮箱 
    *              telephone         (String)座机号 
    *              mobilePhone         (String)手机号 
    *              address         (String)手机号 
    *              createTime         (Date)create_time 
    *              updateTime         (Date)更新时间 
    *              superAdmin         (Integer)是否超级管理员 [{"":"全部"},{"0":"否"},{"1":"是"}]
    *              roleId         (Long)角色 
    *      }]
    *   }
    */
    @RequestMapping(value={"list/{pageIndex}/{pageSize}"}, method={RequestMethod.GET})
    public void list(@PathVariable Integer pageIndex,@PathVariable Integer pageSize,
        @RequestParam(required=false, value="orderBySqlField") String orderBySqlField,
        @RequestParam(required=false, value="descAsc") String descAsc,
        @RequestParam(required=false, value="callback") String callback,
        @RequestParam(required=false, value="encode") String encode,
        @RequestParam(required = false,value ="id")   Long id,
        @RequestParam(required = false,value ="userName")   String userName,
        @RequestParam(required = false,value ="password")   String password,
        @RequestParam(required = false,value ="realName")   String realName,
        @RequestParam(required = false,value ="email")   String email,
        @RequestParam(required = false,value ="telephone")   String telephone,
        @RequestParam(required = false,value ="mobilePhone")   String mobilePhone,
        @RequestParam(required = false,value ="address")   String address,
        @RequestParam(required = false,value ="createTime")      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date createTime,
        @RequestParam(required = false,value ="updateTime")      @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date updateTime,
        @RequestParam(required = false,value ="superAdmin")   Integer superAdmin,
        @RequestParam(required = false,value ="roleId")   Long roleId,
        @RequestParam(required = false,value ="idFirst")                            Long idFirst ,
        @RequestParam(required = false,value ="userNameFirst")                            String userNameFirst ,
        @RequestParam(required = false,value ="realNameFirst")                            String realNameFirst ,
        @RequestParam(required = false,value ="emailFirst")                            String emailFirst ,
        @RequestParam(required = false,value ="createTimeFirst")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date createTimeFirst ,
        @RequestParam(required = false,value ="createTimeSecond")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date createTimeSecond ,
        @RequestParam(required = false,value ="updateTimeFirst")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date updateTimeFirst ,
        @RequestParam(required = false,value ="updateTimeSecond")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date updateTimeSecond ,
        @RequestParam(required = false,value ="superAdminFirst")                            Integer superAdminFirst ,
        @RequestParam(required = false,value ="roleIdFirst")                            String roleIdFirst ,
        HttpServletRequest request,HttpServletResponse response,ModelMap modelMap)
    {
        try{
            String orderBySqlFieldStr = ",id,user_name,password,real_name,email,telephone,mobile_phone,address,create_time,update_time,super_admin,role_id,";
            String orderBy = ProjectUtil.filterOrderBy(orderBySqlFieldStr,orderBySqlField,descAsc);
            if(StringUtils.isBlank(orderBy)){
                orderBy = "";
            }
            pageSize = Math.min(AdminUserConstant.PAGE_MAX_SIZE,pageSize);
            int idx = (pageIndex.intValue() - 1) * pageSize;
            Long _userId = UserWebUtil.userId(request);
            Map<String,Object> query = MapUtil.buildMap("orderBy", orderBy,
         "id",id,
         "userName",userName,
         "password",password,
         "realName",realName,
         "email",email,
         "telephone",telephone,
         "mobilePhone",mobilePhone,
         "address",address,
         "createTime",createTime,
         "updateTime",updateTime,
         "superAdmin",superAdmin,
         "roleId",roleId,
                    "idFirst",idFirst ,
                    "userNameFirst",userNameFirst ,
                    "realNameFirst",realNameFirst ,
                    "emailFirst",emailFirst ,
                    "createTimeFirst",createTimeFirst ,
                    "createTimeSecond",createTimeSecond ,
                    "updateTimeFirst",updateTimeFirst ,
                    "updateTimeSecond",updateTimeSecond ,
                    "superAdminFirst",superAdminFirst ,
                    "roleIdFirst",ProjectUtil.toList(roleIdFirst ,Long.class),
                "start",idx,"limit", pageSize ,"userId",_userId);

            List<AdminUser> pageList = this.adminUserService.getAdminUserList(query);
            toWebSuccessJson(callback,encode,response, coverToResponseList(pageList));
        }catch(Exception e){
            String exceptionMsg = ProjectUtil.buildExceptionMsg(e.getMessage());
            toWebExceptionJson(response,exceptionMsg);
        }
    }

    private List<AdminUserResponse> coverToResponseList(List<AdminUser> pageList){
        if(ListUtils.isBlank(pageList)){
            return null;
        }
        return Copyer.copy(pageList,AdminUserResponse.class);
    }


        /** method:GET
        *   url:/user/admin_user/item?...
        *   demo:/user/admin_user/item
    *   获取管理员表详情数据
    *   是否需要登录                          是
    *是否必须   入参                          注释
    *  否   @callback                      (String)回调方法
    *  否   @encode                        (String)编码 默认UTF-8
     *  否  @id        (Long) 主键
    *  返回
    *   {
    *      code:0,message:"",jsonp:"",
    *      info:
                {
    *              id         (Long)主键 
    *              userName         (String)用户名 
    *              password         (String)密码 
    *              realName         (String)真名 
    *              email         (String)邮箱 
    *              telephone         (String)座机号 
    *              mobilePhone         (String)手机号 
    *              address         (String)手机号 
    *              createTime         (Date)create_time 
    *              updateTime         (Date)更新时间 
    *              superAdmin         (Integer)是否超级管理员 [{"":"全部"},{"0":"否"},{"1":"是"}]
    *              roleId         (Long)角色 
    *      }
    *   }
    */
    @RequestMapping(value={"item"}, method={RequestMethod.GET})
    public void item(
        @RequestParam(required=false, value="callback") String callback,
        @RequestParam(required=false, value="encode") String encode,
        @RequestParam(required = false,value ="id")Long id,
        HttpServletRequest request,HttpServletResponse response,ModelMap modelMap)
    {
        try{
            Long _userId = UserWebUtil.userId(request);
            Map<String,Object> query = MapUtil.buildMap(
                     "id",id,
                "userId",_userId
            );
            AdminUser adminUser = this.adminUserService.selectFirstAdminUser(query);
            toWebSuccessJson(callback,encode,response, Copyer.copy(adminUser,new AdminUserResponse()));
        }catch(Exception e){
            String exceptionMsg = ProjectUtil.buildExceptionMsg(e.getMessage());
            toWebExceptionJson(response,exceptionMsg);
        }
    }

}